﻿/**
 * 窗口类的构造方法。
 */
function MainWindow() {
    var mGame = null                                                                // 游戏
    const mMenuItemEasy = document.getElementById("IDC_MENU_ITEM_EASY")             // "简单"菜单项
    const mMenuItemHard = document.getElementById("IDC_MENU_ITEM_HARD")             // "困难"菜单项
    const mMenuItemHasBorder = document.getElementById("IDC_MENU_ITEM_HAS_BORDER")  // "有"菜单项
    const mMenuItemMedium = document.getElementById("IDC_MENU_ITEM_MEDIUM")         // "中等"菜单项
    const mMenuItemNoBorder = document.getElementById("IDC_MENU_ITEM_NO_BORDER")    // "无"菜单项


    // 设置文档标题
    document.title = IDS_APP_NAME[LANG]

    // 添加游戏结束事件的响应方法
    document.addEventListener(Const.UM_GAME_OVER, onGameOver)

    // 设置"游戏"菜单
    document.getElementById("IDC_MENU_GAME").innerHTML = IDS_GAME[LANG]

    // 设置"开始"菜单项
    const menuItemStart = document.getElementById("IDC_MENU_ITEM_START")
    menuItemStart.innerHTML = IDS_START[LANG]
    menuItemStart.onclick = onMenuItemStartClicked

    // 设置"设置"菜单
    document.getElementById("IDC_MENU_OPTION").innerHTML = IDS_OPTION[LANG]

    // 设置"难度"子菜单
    document.getElementById("IDC_SUBMENU_DIFFICULTY").innerHTML = IDS_SET_DIFFICULTY[LANG]

    // 设置"简单"菜单项
    mMenuItemEasy.onclick = onMenuItemSetDifficultyClicked
    mMenuItemEasy.innerHTML = "√" + IDS_EASY[LANG]

    // 设置"中等"菜单项
    mMenuItemMedium.onclick = onMenuItemSetDifficultyClicked
    mMenuItemMedium.innerHTML = "&nbsp&nbsp" + IDS_MEDIUM[LANG]

    // 设置"困难"菜单项
    mMenuItemHard.onclick = onMenuItemSetDifficultyClicked
    mMenuItemHard.innerHTML = "&nbsp&nbsp" + IDS_HARD[LANG]

    // 设置"图片边框"子菜单
    document.getElementById("IDC_SUBMENU_BORDER").innerHTML = IDS_SET_BORDER[LANG]

    // 设置"有"菜单项
    mMenuItemHasBorder.innerHTML = "√" + IDS_HAS_BORDER[LANG]
    mMenuItemHasBorder.onclick = onMenuItemSetBorderClicked

    // 设置"无"菜单项
    mMenuItemNoBorder.innerHTML = "&nbsp&nbsp" + IDS_NO_BORDER[LANG]
    mMenuItemNoBorder.onclick = onMenuItemSetBorderClicked

    // 设置"换图片"菜单项
    const menuItemSetPicture = document.getElementById("IDC_MENU_ITEM_SET_PICTURE")
    menuItemSetPicture.innerHTML = IDS_SET_PICTURE[LANG]
    menuItemSetPicture.onclick = onMenuItemSetPictureClicked

    // 创建游戏区域
    const gameBoard = document.getElementById('IDC_GAME_BOARD')
    const gameBoardCanvas = new GameBoard('IDC_GAME_BOARD_CANVAS')

    // 创建游戏
    mGame = new Game(gameBoardCanvas.getDrawer(), gameBoard, gameBoardCanvas)
    
    /**
     * 游戏结束事件的响应方法。
     */
    function onGameOver() {
        // 显示胜利提示信息
        window.alert(IDS_WON[LANG])
    }

    /**
     * "图片边框"菜单项的单击响应方法。
     * @param e 事件相关信息
     */
    function onMenuItemSetBorderClicked(e) {
        // 如果要设置的参数与当前游戏参数相同，则直接返回
        const hasBorder = e.srcElement.id == "IDC_MENU_ITEM_HAS_BORDER"
        if (hasBorder == mGame.pictureHasBorder()) {
            return
        }

        // 设置边框
        mGame.setPictureBorder(hasBorder)

        // 选中菜单项
        if (hasBorder) {
            mMenuItemHasBorder.innerHTML = "√" + IDS_HAS_BORDER[LANG]
            mMenuItemNoBorder.innerHTML = "&nbsp&nbsp" + IDS_NO_BORDER[LANG]
        } else {
            mMenuItemHasBorder.innerHTML = "&nbsp&nbsp" + IDS_HAS_BORDER[LANG]
            mMenuItemNoBorder.innerHTML = "√" + IDS_NO_BORDER[LANG]
        }
    }

    /**
     * "难度"菜单项的单击响应方法。
     * @param e 事件相关信息
     */
    function onMenuItemSetDifficultyClicked(e) {
        // 如果要设置的参数与当前游戏参数相同，则直接返回
        var newDiff = GameDifficulty.EASY
        if (e.srcElement.id == "IDC_MENU_ITEM_MEDIUM") {
            newDiff = GameDifficulty.MEDIUM
        } else if (e.srcElement.id == "IDC_MENU_ITEM_HARD") {
            newDiff = GameDifficulty.HARD
        }
        if (newDiff == mGame.getDifficulty()) {
            return
        }

        // 弹出询问对话框
        if (mGame.getStatus() != GameStatus.END) {
            const clickedButton = window.confirm(IDS_RESET[LANG])
            if (!clickedButton) {
                return
            }
        }

        // 设置难度
        mGame.setDifficulty(newDiff)

        // 选中菜单项
        mMenuItemEasy.innerHTML = "&nbsp&nbsp" + IDS_EASY[LANG]
        mMenuItemMedium.innerHTML = "&nbsp&nbsp" + IDS_MEDIUM[LANG]
        mMenuItemHard.innerHTML = "&nbsp&nbsp" + IDS_HARD[LANG]
        if (newDiff == GameDifficulty.EASY) {
            mMenuItemEasy.innerHTML = "√" + IDS_EASY[LANG]
        } else if (newDiff == GameDifficulty.MEDIUM) {
            mMenuItemMedium.innerHTML = "√" + IDS_MEDIUM[LANG]
        } else if (newDiff == GameDifficulty.HARD) {
            mMenuItemHard.innerHTML = "√" + IDS_HARD[LANG]
        }
    }

    /**
     * "换图片"菜单项的单击响应方法。
     */
    function onMenuItemSetPictureClicked() {
        // 弹出询问对话框
        if (mGame.getStatus() != GameStatus.END) {
            var result = window.confirm(IDS_RESET[LANG])
            if (!result) {
                return
            }
        }

        // 弹出选择图片对话框
        const fileDialog = document.getElementById("IDC_FILE_DIALOG")
        fileDialog.onchange = function() {
            // 设置图片
            var imagePath = window.URL.createObjectURL(fileDialog.files[0])
            mGame.setPicture(imagePath)
        } 
        fileDialog.click()
    }

    /**
     * "开始"菜单项的单击响应方法。
     */
    function onMenuItemStartClicked() {
        mGame.start()
    }
}
